home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Documentation / Books / Learn Java on the Macintosh / Learn Java Projects / 11.01 - components / UIApplet.java < prev    next >
Text File  |  1996-04-22  |  2KB  |  88 lines

  1. /* -------------------------------------------------------------
  2. This applet creates a few different user interface components
  3. and detects when the user interacted with them.
  4.  
  5. Java's classes: Applet        (applet)
  6.                 System        (lang)
  7.                 Button        (awt)
  8.                 Choice        (awt)
  9.                 TextField     (awt)
  10.                 Checkbox      (awt)
  11.                 CheckboxGroup (awt)
  12.                 Label         (awt)
  13.                 Event         (awt)
  14.  
  15. Custom classes: UIApplet
  16.  
  17. ------------------------------------------------------------- */
  18. import java.awt.*;
  19.  
  20. public class UIApplet extends java.applet.Applet {
  21.  
  22.    Button         button;
  23.    Choice         choice;
  24.    TextField      textField;
  25.  
  26.    /** Create a user interface. */
  27.    public void init() {
  28.  
  29.       Checkbox       checkbox;
  30.       CheckboxGroup  checkboxGroup;   
  31.       Label          label;
  32.       
  33.       // create a choice list
  34.       choice = new Choice();
  35.       choice.addItem("Apple");
  36.       choice.addItem("Banana");
  37.       choice.addItem("Cherry");
  38.       add(choice);
  39.  
  40.       // create a text field
  41.       textField = new TextField(10); // 10 columns wide
  42.       add(textField);
  43.  
  44.       // create a button
  45.       button = new Button("Click me");
  46.       add(button);
  47.       
  48.       // create a label
  49.       label = new Label("I am a label");
  50.       add(label);
  51.       
  52.       // create 3 exlusive-choice checkboxes 
  53.       checkboxGroup = new CheckboxGroup();
  54.       
  55.       checkbox = new Checkbox("Yes", checkboxGroup, false);
  56.       add(checkbox);
  57.       checkbox = new Checkbox("No", checkboxGroup, false);
  58.       add(checkbox);
  59.       checkbox = new Checkbox("Maybe", checkboxGroup, true);
  60.       add(checkbox);
  61.             
  62.    }   
  63.       
  64.    /** Respond to user input events. */
  65.    public boolean action(Event e, Object arg) {
  66.             
  67.         if (e.target == textField)
  68.            System.out.println("User entered text into the text field");
  69.            
  70.         else if (e.target == button)
  71.            System.out.println("User clicked the button");
  72.                       
  73.         else if (e.target == choice)
  74.            System.out.println("User selected a new choice");
  75.  
  76.         else if (e.target instanceof Checkbox)
  77.            System.out.println("User clicked a check box");
  78.         
  79.         else
  80.            System.out.println("Unrecognized event");
  81.         
  82.         return super.action(e, arg);
  83.         
  84.    }
  85.  
  86. }
  87.  
  88.